package com.kb.design.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kb.design.entity.DesignResponsiblePerson;
import com.kb.design.entity.DesignRole;
import com.kb.design.entity.ProjectQaValue;
import com.kb.design.entity.QaCheckDict;
import com.kb.design.enums.DesignStatus;
import com.kb.design.enums.UserRole;
import com.kb.design.mapper.postgreSQL.DesignResponsiblePersonMapper;
import com.kb.design.mapper.postgreSQL.ProjectQaValueMapper;
import com.kb.design.mapper.postgreSQL.QaCheckDictMapper;
import com.kb.design.service.QaCheckDictService;
import org.aspectj.weaver.ast.Var;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;;
import java.util.*;
import java.util.stream.Collectors;

@Service
@DS("slave-1")
public class QaCheckDictServiceImpl extends ServiceImpl<QaCheckDictMapper, QaCheckDict> implements QaCheckDictService {
    @Resource
    private QaCheckDictMapper qaCheckDictMapper;
    @Resource
    private ProjectQaValueMapper projectQaValueMapper;
    @Resource
    private DesignResponsiblePersonMapper responsiblePersonMapper;
    @Override
    public void insert(QaCheckDict qaCheckDict) {
        qaCheckDictMapper.insert(qaCheckDict);
    }

    @Override
    public void qaUpdate(QaCheckDict qaCheckDict) {
        qaCheckDictMapper.updateById(qaCheckDict);
    }

    @Override
    public void qaDel(QaCheckDict qaCheckDict) {
        qaCheckDictMapper.deleteById(qaCheckDict);
    }

    @Override
    public Page<QaCheckDict> queryList(Page<QaCheckDict> page, QaCheckDict qaCheckDict) {
        LambdaQueryWrapper<QaCheckDict> queryWrapper = Wrappers.lambdaQuery(QaCheckDict.class);
        if (qaCheckDict.getImportance() != null) {
            queryWrapper.eq(QaCheckDict::getImportance, qaCheckDict.getImportance());
        }
        if (qaCheckDict.getFunctionalType() != null) {
            queryWrapper.eq(QaCheckDict::getFunctionalType, qaCheckDict.getFunctionalType());
        }
        if (qaCheckDict.getProjectType() != null) {
            queryWrapper.like(QaCheckDict::getProjectType, qaCheckDict.getProjectType());
        }
        queryWrapper.orderByAsc(QaCheckDict::getId);
        return qaCheckDictMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<Map<String, Object>> getGroupedByProjectAndType(QaCheckDict qaCheckDict) {
        if (qaCheckDict.getRoleKey() != null) {
            //根据当前登录人在项目中的角色进行检查列表返回 用流程步骤数据判断
            String createBy = qaCheckDict.getCreateBy();
            Long projectId = qaCheckDict.getProjectId();
            LambdaQueryWrapper<DesignResponsiblePerson> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(DesignResponsiblePerson::getProjectId, projectId)
                    .eq(DesignResponsiblePerson::getResponsiblePersonId, createBy)
                    .eq(DesignResponsiblePerson::getDel, "0");
            List<DesignResponsiblePerson> responsiblePeople = responsiblePersonMapper.selectList(wrapper);
            //根据node_status  person_type赋值
            if (CollectionUtils.isNotEmpty(responsiblePeople)){
                DesignResponsiblePerson designResponsiblePerson = responsiblePeople.get(0);
                if (responsiblePeople.size() > 1){
                    //一人多角色
                    if (Objects.equals(designResponsiblePerson.getNodeStatus(), DesignStatus.ENCAPSULATION.getIndex())){
                        qaCheckDict.setFunctionalType("1");
                    }else if (Objects.equals(designResponsiblePerson.getNodeStatus(), DesignStatus.DESIGN.getIndex())){
                        qaCheckDict.setFunctionalType("2");
                    }
                    //封装主管
                }else if (Objects.equals(designResponsiblePerson.getNodeStatus(), DesignStatus.ENCAPSULATION.getIndex()) && Objects.equals(designResponsiblePerson.getPersonType(), 1)){
                    qaCheckDict.setFunctionalType("1");
                    qaCheckDict.setImportance("2");
                    //封装工程师  检查所有
                }else if (Objects.equals(designResponsiblePerson.getNodeStatus(), DesignStatus.ENCAPSULATION.getIndex()) && Objects.equals(designResponsiblePerson.getPersonType(), 2)){
                    qaCheckDict.setFunctionalType("1");
                    //设计主管
                }else if (Objects.equals(designResponsiblePerson.getNodeStatus(), DesignStatus.DESIGN.getIndex()) && Objects.equals(designResponsiblePerson.getPersonType(), 1)){
                    qaCheckDict.setFunctionalType("2");
                    qaCheckDict.setImportance("2");
                    //设计工程师  检查所有
                }else if (Objects.equals(designResponsiblePerson.getNodeStatus(), DesignStatus.DESIGN.getIndex()) && Objects.equals(designResponsiblePerson.getPersonType(), 2)){
                    qaCheckDict.setFunctionalType("2");
                }else if (Objects.equals(designResponsiblePerson.getNodeStatus(), DesignStatus.CHECK.getIndex()) && Objects.equals(designResponsiblePerson.getPersonType(), 1)){
                    qaCheckDict.setFunctionalType("3");
                }else {
                    qaCheckDict.setImportance("2");
                    qaCheckDict.setFunctionalType("2");
                }
            }


            //角色转换 角色是可能多个 类似 a,b 当有封装工程师角色时  qaCheckDict.setFunctionalType("1"); 当仅有主管时  1 2
//            if (UserRole.PAGE_ENGINEERS.getKey().equals(qaCheckDict.getRoleKey())) {
////                qaCheckDict.setImportance("1");
//                qaCheckDict.setFunctionalType("1");
//            }else if (UserRole.PAGE_COMPETENT.getKey().equals(qaCheckDict.getRoleKey())) {
//                qaCheckDict.setImportance("2");
//                qaCheckDict.setFunctionalType("1");
//            } else if (qaCheckDict.getRoleKey().contains(UserRole.PAGE_COMPETENT.getKey()) && qaCheckDict.getRoleKey().contains(UserRole.PAGE_ENGINEERS.getKey())) {
//                qaCheckDict.setFunctionalType("1");
//            }
//            else if (UserRole.DESIGN_ENGINEERS.getKey().equals(qaCheckDict.getRoleKey())) {
////                qaCheckDict.setImportance("1");
//                qaCheckDict.setFunctionalType("2");
//            } else if (qaCheckDict.getRoleKey().contains(UserRole.DESIGN_ENGINEERS.getKey()) && qaCheckDict.getRoleKey().contains(UserRole.DESIGN_COMPETENT.getKey())) {
////                qaCheckDict.setImportance("2");
//                qaCheckDict.setFunctionalType("2");
//            }
//            else if (UserRole.DESIGN_COMPETENT.getKey().equals(qaCheckDict.getRoleKey())) {
//                qaCheckDict.setImportance("2");
//                qaCheckDict.setFunctionalType("2");
//            } else if (UserRole.VALOR.getKey().equals(qaCheckDict.getRoleKey())) {
//                qaCheckDict.setFunctionalType("3");
//            }
//            else if (UserRole.ADMIN.getKey().equals(qaCheckDict.getRoleKey())) {
//                qaCheckDict.setImportance("2");
//                qaCheckDict.setFunctionalType("2");
//            }

            LambdaQueryWrapper<QaCheckDict> queryWrapper = Wrappers.lambdaQuery(QaCheckDict.class);
            queryWrapper.eq(QaCheckDict::getFunctionalType, qaCheckDict.getFunctionalType());
            if (qaCheckDict.getImportance() != null){
                queryWrapper.eq(QaCheckDict::getImportance, qaCheckDict.getImportance());
            }
            List<QaCheckDict> qaCheckDictList = qaCheckDictMapper.selectList(queryWrapper);
            return data(qaCheckDictList);
        }
        return Collections.emptyList();
    }

    @Override
    public List<Map<String, Object>> getDraft(QaCheckDict qaCheckDict) {
        List<ProjectQaValue> projectQaValues = projectQaValueMapper.selectList(Wrappers.lambdaQuery(ProjectQaValue.class)
                .eq(ProjectQaValue::getProjectId, qaCheckDict.getProjectId())
                .eq(ProjectQaValue::getCreateBy, qaCheckDict.getCreateBy()));
        List<QaCheckDict> qaCheckDicts = new ArrayList<>();
        for (ProjectQaValue projectQaValue : projectQaValues) {
            qaCheckDict = BeanUtil.copyProperties(projectQaValue, QaCheckDict.class);
            qaCheckDict.setCheckContents(projectQaValue.getText());
//            qaCheckDict.setQtValue(projectQaValue.getQtValue());
            qaCheckDicts.add(qaCheckDict);
        }
        return data(qaCheckDicts);
    }

    @Override
    public boolean draft(QaCheckDict qaCheckDict) {
        List<ProjectQaValue> projectQaValues = projectQaValueMapper.selectList(Wrappers.lambdaQuery(ProjectQaValue.class)
                .eq(ProjectQaValue::getProjectId, qaCheckDict.getProjectId())
                .eq(ProjectQaValue::getCreateBy, qaCheckDict.getCreateBy()));
        return !projectQaValues.isEmpty();
    }
    public List<Map<String, Object>> data(List<QaCheckDict> qaCheckDictList){
        return qaCheckDictList.stream()
                .collect(Collectors.groupingBy(
                        QaCheckDict::getProjectType,
                        Collectors.groupingBy(
//                                    qa -> qa.getType() != null ? qa.getType() : qa.getProjectType(),
                                qa -> (qa.getType() == null || qa.getType().isEmpty()) ? qa.getProjectType() : qa.getType(),
                                Collectors.mapping(qa -> {
                                    Map<String, Object> checkContentMap = new HashMap<>();
                                    checkContentMap.put("text", qa.getCheckContents());
                                    checkContentMap.put("importance", qa.getImportance());
                                    checkContentMap.put("functionalType", qa.getFunctionalType());
                                    checkContentMap.put("qaId", qa.getId());
                                    if (qa.getQaId() != null){
                                        checkContentMap.put("qaId", qa.getQaId());
                                    }
                                    checkContentMap.put("qtValue", qa.getQtValue());
                                    checkContentMap.put("remark", qa.getRemark());
                                    return checkContentMap;
                                }, Collectors.toList())
                        )
                ))
                .entrySet().stream()
                .map(entry -> {
                    Map<String, Object> projectTypeMap = new HashMap<>();
                    projectTypeMap.put("projectType", entry.getKey());

                    List<Map<String, Object>> typeList = entry.getValue().entrySet().stream()
                            .map(subEntry -> {
                                Map<String, Object> typeMap = new HashMap<>();
                                typeMap.put("type", subEntry.getKey());
                                typeMap.put("checkContents", subEntry.getValue());
                                return typeMap;
                            })
                            .collect(Collectors.toList());

                    projectTypeMap.put("type", typeList);
                    return projectTypeMap;
                })
                .collect(Collectors.toList());
    }
}
